Java Projects by Peter Verhas
Author:Peter Verhas
Language: eng
Format: epub
Publisher: Packt
Published: 2018-08-31T06:32:02+00:00
BlockingQueue
BlockingQueue is an interface that extends the standard Queue interface with methods that are suitable for use by multithread applications. Any implementation of this interface provides methods that allow different threads to put an element into the queue, pull elements off the queue, and wait for elements that are in the queue.
When there is a new element to be stored in the queue, you can add() it, offer() it, or put() it. These are the names of the methods that store elements and they do the same thing, just a bit differently. The add() method throws an exception if the queue is full and there is no room for the element. The offer() method does not throw an exception but returns either true or false, depending on whether the operation is successful. If it can store the element in the queue, it returns true. There is also a version of offer() that specifies a timeout. That version of the method waits and returns only false if it cannot store the value in the queue during the period. The put() method is the simplest version; it waits until it can do its job.
When talking about the available room in a queue, do not get puzzled and confuse it with general Java memory management. If there is no more memory, and the garbage collector is also unable to release any, you will certainly get an OutOfMemoryError. An exception is thrown by add(), and the false value is returned by offer() when the queue limits are reached. Some of the BlockingQueue implementations can limit the number of elements that can be stored in a queue simultaneously. If that limit is reached, then the queue is full and cannot accept more elements.
There are four different ways of fetching elements from a BlockingQueue implementation. In this direction, the special case is when the queue is empty. In that case, the remove() method throws an exception instead of returning the element, the poll() method returns null if there is no element, and the take() method just waits until it can return an element.
Finally, there are two methods inherited from the Queues interface that do not consume the element from the queue, but just look at it. The element() method returns the head of the queue or throws an exception if the queue is empty. The peek() method returns null if there is no element in the queue. The following table summarizes the operations borrowed from the documentation of the interface:
Throws exception Special value Blocks Times out
Insert add(e) offer(e) put(e) offer(e, time, unit)
Remove remove() poll() take() poll(time, unit)
Examine element() peek() not applicable not applicable
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12568)
Hello! Python by Anthony Briggs(9912)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9336)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8293)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7040)
Microservices with Go by Alexander Shuiskov(6805)
Practical Design Patterns for Java Developers by Miroslav Wengner(6717)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6656)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Angular Projects - Third Edition by Aristeidis Bampakos(6065)
The Art of Crafting User Stories by The Art of Crafting User Stories(5596)
NetSuite for Consultants - Second Edition by Peter Ries(5531)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5335)
Kotlin in Action by Dmitry Jemerov(5062)
